const originRequest = require("request"); // 请求
const iconv = require("iconv-lite"); //编码
const cheerio = require("cheerio"); //node jquery

const { addOne } = require("../models/spider");
const test = () => {
  originRequest("https://www.baidu.com", function (error, response, body) {
    console.log(body); //打印百度首页html内容
  });
};
// https://www.dy2018.com
const sigleRequest = (url) => {
  let option = {
    encoding: null,
  };
  return new Promise((resolve, reject) => {
    originRequest(url, option, function (err, res, body) {
      if (err) {
        reject(err);
      }
      // console.log(body);
      // test()
      resolve(body);
    });
  });
};

const handleSpider = async (url) => {
  let res = await sigleRequest(url);
  console.log(res);
  const html = iconv.decode(res, "gb2312");
  // console.log(html);
  const $ = cheerio.load(html);
  let resArr = [];
  $(".co_content222 li a").each(async (i, elem) => {
    let obj = {};
    obj.id = Number(i);
    const curtime = new Date();
    obj.updateTime = curtime;
    obj.name = $(elem).text();
    resArr.push(obj);
    // console.log($(elem).text());
    let result = await addOne(obj);
    // console.log(result);
  });

  return resArr;
};
module.exports = {
  handleSpider,
};
